using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CosyTrade.UI;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace CosyTrade.StoreManagement.UI
{
    public partial class ProductCategoryList : XtraUserControl
    {
        private IProductManager productMgr;
        public ProductCategoryList()
        {
            InitializeComponent();
        }

        public void Initialize(IProductManager _productMgr)
        {
            productMgr = _productMgr;
            LoadBaseData();
        }

        protected override void OnFirstLoad()
        {
            base.OnFirstLoad();
            if (DesignMode) return;
            if (ParentForm.GetType().IsSubclassOf(typeof(AddinFormBase)))
            {
                productMgr = ((AddinFormBase) ParentForm).CurrentPortalFactory.GetProxy<IProductManager>();
                LoadBaseData();
            }
        }

        public event EventHandler OnSelected;
        public event EventHandler OnCancel;

        public ProductCategoryBase SelectProductCategory
        {
            get
            {
                if (treeList1.Selection.Count > 0) return (ProductCategoryBase) treeList1.Selection[0].GetValue("Me");
                else return null;
            }
        }

        private void LoadBaseData()
        {
            treeList1.ClearNodes();
            IList<ProductCategoryBase> productList = productMgr.GetProductCategoryRootlist();
            foreach (ProductCategoryBase categoryBase in productList)
            {
                TreeListNode rootNode = treeList1.AppendNode(
                    new object[] {categoryBase.CategoryChinese, categoryBase.CategorySpanish, categoryBase}, null);
                rootNode.HasChildren = true;
                rootNode.Tag = false;
                //treeList1.Nodes.Add(rootNode);
            }
        }

        private void treeList1_BeforeExpand(object sender, BeforeExpandEventArgs e)
        {
            if ((bool)e.Node.Tag) return;
            e.Node.Tag = true;
            ProductCategoryBase productCategory =  (ProductCategoryBase) e.Node.GetValue("Me");
            
            if (productCategory == null)
            {
                e.CanExpand = false;
                e.Node.HasChildren = false;
                return;
            }
            ProductCategory productCategoryDetail = productMgr.GetProductCategoryDetail(productCategory.PrimaryKey);
            foreach (ProductCategoryBase category in productCategoryDetail.Childs)
            {
                TreeListNode childNode = treeList1.AppendNode(
                    new object[] {category.CategoryChinese, category.CategorySpanish, category}, e.Node);
                childNode.HasChildren = true;
                childNode.Tag = false;
                //treeList1.Nodes.Add(childNode);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (OnCancel != null) OnCancel(this, new EventArgs());
        }

        private void treeList1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TreeListHitInfo clickInfo = treeList1.CalcHitInfo(e.Location);
            if (clickInfo.Node != null && OnSelected != null) OnSelected(this, new EventArgs());
        }
    }
}
